#include <master.h>
#include <debug.h>
#include <cpuid.h>
#include <syscall.h>

// :)

// this is set here
int syscall_method = 0;

void do_cpuid_test() {
 char vendorstr[16];
 cpuid_info_t id;

 if (cpuid_present() == 0) {
  printf("For some reason your CPU does not support CPUID.\n"
         "This usually means you are a fucking cheapskate. Go and buy a new PC.\n");
  panic("user is too cheap for this OS");
 }
 
 cpuid_vendor(vendorstr);
 printf("We are running on a %s\n", vendorstr);

 cpuid_info(1, &id);
 printf("After CPUID(0x1): %08x %08x %08x %08x\n", id.eax, id.ebx, id.ecx, id.edx);

 if ((id.edx & CPUID_NEEDED_MASK) != CPUID_NEEDED_MASK) {
  printf("I'm tired of your shit. Get a real CPU.\nCPUID missing: %08x\n", 
	CPUID_NEEDED_MASK | ~(id.edx & CPUID_NEEDED_MASK));

  panic("user is a cheapskate");
 }

 // wait. what?
 // intel cpus wont report SYSENTER unless they are in x64 mode.
 // well, fuck them.
 if ((id.edx & CPUID_IA64) || (id.edx & CPUID_SEP)) {
  printf("Sysenter selected as syscall method\n");
  syscall_method = SYSCALL_USE_SYSENTER;
 }

 if (strcmp(vendorstr, "AuthenticAMD") == 0) {
   cpuid_info(0x80000001, &id);
   printf("extended AMD cpuid: %08x %08x %08x %08x\n", id.eax, id.ebx, id.ecx, id.edx);
      
   if ((id.edx & CPUID_SEP) && (syscall_method == 0)) syscall_method = SYSCALL_USE_SYSCALL;
   printf("Syscall selected as syscall method\n");

 }

 if (syscall_method == 0) 
   printf("Interrupt selected as syscall method\n");

}

